<?php
class Default_AuthController extends Zend_Controller_Action
{

	function loginAction()
	{
		$this->view->message = '';

		if ($this->_request->isPost()) {
			// collect the data from the user
			$f = new Zend_Filter_StripTags();
			$login = $f->filter($this->_request->getPost('login'));
			$password = $f->filter($this->_request->getPost('password'));
				
			if (empty($login)) {
				$this->view->message = 'Please provide a username.';
			} else {
				// setup Zend_Auth adapter for a database table
				$us_obj=new Application_Model_Db_Users();
				$auth=new Zend_Auth_Adapter_DbTable($us_obj->getAdapter());
				$auth
					->setTableName('users')
					->setIdentityColumn('email')
					->setCredentialColumn('password');
				// Set the input credential values
				// to authenticate against
				$auth
					->setIdentity($login)
					->setCredential($password);
				
				$result = $auth->authenticate();
				$data = $auth->getResultRowObject(null, 'password');
				Zend_Auth::getInstance()->getStorage()->write($data);
			}
		}
	}
	function logoutAction(){
		Zend_Auth::getInstance()->clearIdentity();
		$this->_redirect('/');
	}
}